﻿using FreeSql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP10
{
    public abstract class LoadingActionBase : PLCAction
    {
        protected LoadingActionBase(DecodeValue _value) : base(_value)
        {
        }


        /*
         
         ===========子篮类别=========
            1=翅片QXA；
            2=盖板QXK；
            3=底板QXM；
            4=水嘴焊环大QXW；
            5=定位柱焊环小QXZ；
            6=焊丝QXX；
            7=焊片QXY；
            8=中板QXH；
            9=接头QXN；
            10=定位柱QXG
            =========L1.L2.L3.L4输送定义========
            L1上料线:1=翅片QXA；
            L2上料线:2=盖板QXK；3=底板QXM；
            L3上料线:4=水嘴焊环大QXW；5=定位柱焊环小QXZ；6=焊丝QXX：7=焊片QXY；
            L4上料线:8=中板QXH；9=接头QXN；10=定位柱QXG
         */

        public void ProccessLoading(int replayDB, int replayAction, string bluecode)
        {
            var plan = LoadingPlanHelper.GetLoadingPlan(conn);
            int PartType = GetPartType(bluecode);
            var needTypePos = Value.GetPosition(replayDB, replayAction, 2);
            var replayPos = Value.GetPosition(replayDB, replayAction, 1);
            int needType = 0;
            bool Success = false;
            if (plan != null)
            {

                var planPart = conn.Select<CksLoadingPlanDetails>()
                     .Where(a => a.PlanId == plan.Id && a.PartType == PartType).First();
                if (planPart != null && planPart.Status == 1)
                {

                    //可以上料，写入1
                    AckWriteInt16(replayPos, 1);

                    Success = true;
                }
                else if(planPart != null)
                {
                    //不可以上料，
                    //查询当前计划的未完成的推荐零件
                    var d = conn.Select<CksLoadingPlanDetails>()
                       .Where(a => a.PlanId == plan.Id && a.Status == 1 && a.Line == planPart.Line)
                       .OrderBy(a => a.Order).First();
                    needType = d == null ? 0 : d.PartType;
                }

            }
            if (!Success)
            {
                AckWriteInt16(needTypePos, (short)needType);
                AckWriteInt16(replayPos, 2);
            }
            
        }

        /// <summary>
        /// 更新上料计划
        /// </summary>
        /// <param name="oldBasketStatus"></param>
        /// <param name="PartType"></param>
        public void UpdateLoadingPlan(int oldBasketStatus, int PartType)
        {
            var plan = LoadingPlanHelper.GetLoadingPlan(conn);
            if (plan != null)
            {
                var planPart = conn.Select<CksLoadingPlanDetails>()
                            .Where(a => a.PlanId == plan.Id && a.PartType == PartType).First();
                if (planPart != null && oldBasketStatus != 1)
                {
                    planPart.RealCount++;
                    planPart.LastTime = DateTime.Now;
                    if (planPart.PlanCount <= planPart.RealCount)
                        planPart.Status = 10;
                    conn.Update<CksLoadingPlanDetails>()
                        .SetSource(planPart).UpdateColumns(a => new { a.RealCount, a.LastTime, a.Status })
                        .ExecuteAffrows();
                    //检查计划表是否完成
                    if (!conn.Select<CksLoadingPlanDetails>()
                        .Where(a => a.PlanId == plan.Id && a.Status == 1).Any())
                    {
                        //当前上料计划已经完成
                        plan.Status = 10;
                        plan.EndTime = DateTime.Now;
                        conn.Update<CksLoadingPlan>().SetSource(plan)
                            .UpdateColumns(a => new { a.Status, a.EndTime }).ExecuteAffrows();
                    }

                }
            }
        }


    }
}
